package com.admin.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.admin.bean.GameDataBean;
import com.admin.util.SqlUtil;

@Repository
public class GameDataDao {

	@Autowired private JdbcTemplate jdbcTemplate;
	@Autowired private QueryRunner queryRunner;
	public int addGameDataBean(GameDataBean bean) {
		String sql = "insert into admingamedata(cpid,data1,data2,data3,data4,dt,spid,cpdata1,cpdata2,cpdata3,cpdata4) values(?,?,?,?,?,?,?,?,?,?,?)";
		return jdbcTemplate.update(sql, new Object[]{bean.getCpid(),bean.getData1(),bean.getData2(),bean.getData3(),bean.getData4(),bean.getDt(),bean.getSpid(),bean.getCpdata1(),bean.getCpdata2(),bean.getCpdata3(),bean.getCpdata4()});
	}
	
	
	public List<GameDataBean> findDataByCpid(int cpid,int start,int limit)  throws SQLException{
		
		String condition = "";
		if(cpid>0) {
			condition = " cpid = "+cpid;
		} else {
			condition = " 1=1";
		}
		String sql = SqlUtil.getPageSqlForSqlServerDesc("*", "id", "admingamedata", "dt", condition, start, limit);
		
		return queryRunner.query(sql, new BeanListHandler<GameDataBean>(GameDataBean.class),new Object[]{});
	}
	
	
	
	
	
	public int findDataCountByCpid(int cpid) throws SQLException {
		String condition = "";
		if(cpid>0) {
			condition = " cpid = "+cpid;
		} else {
			condition = " 1=1";
		}
		
		String sql = "select count(0) from admingamedata where "+ condition;
		return jdbcTemplate.queryForInt(sql);
	}
	
	public List<GameDataBean> findDataByCondition(String condition)  throws SQLException{
		
		String sql = "select * from admingamedata "+ condition;
		
		return queryRunner.query(sql, new BeanListHandler<GameDataBean>(GameDataBean.class),new Object[]{});
	}
	
	public List<GameDataBean> findVideoByCondition(String condition)  throws SQLException{
		
		String sql = "select * from adminvideodata "+ condition;
		
		return queryRunner.query(sql, new BeanListHandler<GameDataBean>(GameDataBean.class),new Object[]{});
	}
	
	public int addVideoDataBean(GameDataBean bean) {
		String sql = "insert into adminvideodata(cpid,data1,data2,data3,data4,dt,spid,cpdata1,cpdata2,cpdata3,cpdata4) values(?,?,?,?,?,?,?,?,?,?,?)";
		return jdbcTemplate.update(sql, new Object[]{bean.getCpid(),bean.getData1(),bean.getData2(),bean.getData3(),bean.getData4(),bean.getDt(),bean.getSpid(),bean.getCpdata1(),bean.getCpdata2(),bean.getCpdata3(),bean.getCpdata4()});
	}
	
	
	public List<GameDataBean> findVideoByCpid(int cpid,int start,int limit)  throws SQLException{
		
		String condition = "";
		if(cpid>0) {
			condition = " cpid = "+cpid;
		} else {
			condition = " 1=1";
		}
		String sql = SqlUtil.getPageSqlForSqlServerDesc("*", "id", "adminvideodata", "dt", condition, start, limit);
		
		return queryRunner.query(sql, new BeanListHandler<GameDataBean>(GameDataBean.class),new Object[]{});
	}
	
	public int findVideoCountByCpid(int cpid) throws SQLException {
		String condition = "";
		if(cpid>0) {
			condition = " cpid = "+cpid;
		} else {
			condition = " 1=1";
		}
		
		String sql = "select count(0) from adminvideodata where "+ condition;
		return jdbcTemplate.queryForInt(sql);
	}
}
